package com.cn.boot.spring.controller;

import com.cn.boot.spring.entity.UserEntity;
import com.cn.boot.spring.service.IUserService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户管理")
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private IUserService iUserService;
    @ApiOperation("用户列表")
    @GetMapping("/findAllUser")
    public List<UserEntity> findAllUser(@RequestBody UserEntity userEntity) {
        return iUserService.findAllUser(userEntity);

    }
    @ApiOperation("根据某一id查询用户1")
    @GetMapping("/findById1")
    public UserEntity findById1(@RequestParam Integer id) {
        return iUserService.findById(id);
    }
    @ApiOperation("根据某一id查询用户2")
    @GetMapping("/findById2/{id}")
    public UserEntity findById2(@PathVariable Integer id) {
        return iUserService.findById(id);
    }


    /**
     * 添加一条数据
     * Post请求要设置请求方法，参数注解要用@RequestBody
     */
    @ApiOperation("添加一个用户")
    @PostMapping("/addUser")
    public Integer addUser(@RequestBody UserEntity userEntity) {
        return iUserService.addUser(userEntity).getId();
    }

    /**
     * 批量添加
     * 参数传入list
     */
    @ApiOperation("批量添加用户")
    @PostMapping("/addUsers")
    public Integer addUsers(@RequestBody List<UserEntity> users) {
        iUserService.addUsers(users);
//        System.out.println(users);
        //添加几条就返回几条
        return users.size();
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除用户")
    @DeleteMapping("/delUserByIds")
    public Integer delUserByIds(@RequestBody List<Integer> ids) {
        iUserService.delUserByIds(ids);
        //删除几条就返回几条
        return ids.size();
    }
    /**
     * 更新
     */
    @ApiOperation("更新用户数据")
    @PostMapping("/updateUser")
    public Integer updateUser(@RequestBody UserEntity userEntity){
        return iUserService.updateUser(userEntity);
    }
    /**
     * 分页查询
     */
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public Page<UserEntity> page(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
                return iUserService.page(pageNum,pageSize);
    }

    @GetMapping("/findAll")
    @PreAuthorize("hasAuthority('wx:product:read')")
    public List<UserEntity> findAll(){
        return iUserService.findAll();
    }
}
